﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Menu/MenuBar Object - 方法 &amp; 属性 | AutoHotkey v2</title>
<meta name="description" content="The Menu/MenuBar object provides an interface for menus or menu bars to perform actions, such as adding menu items, or to retrieve or set values, such as the default menu item." />
<meta name="ahk:equiv-v1" content="commands/Menu.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css">
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Menu/MenuBar Object</h1>
<p>Used to modify and display menus or menu bars.</p>
<p>Menu objects are used to define, modify and display popup menus. <a href="../commands/MenuCreate.htm">MenuCreate</a>, <a href="../commands/MenuFromHandle.htm">MenuFromHandle</a> and <a href="../Variables.htm#TrayMenu">A_TrayMenu</a> return an object of this type.</p>
<p>MenuBar objects are used to define and modify menu bars for use with <a href="Gui.htm#MenuBar">Gui.MenuBar</a>. <a href="../commands/MenuFromHandle.htm">MenuFromHandle</a> returns an object of this type if given a menu bar handle.</p>

<p><strong>属性:</strong></p>
<ul>
  <li><a href="#ClickCount">ClickCount</a>: Retrieves or sets the number of clicks required to activate the tray menu's default item.</li>
  <li><a href="#Default">Default</a>: Retrieves or sets the default menu item.</li>
  <li><a href="#Handle">Handle</a>: Retrieves the menu's Win32 handle.</li>
</ul>
<p><strong>方法:</strong></p>
<ul>
  <li><a href="#Add">Add</a>: Adds or modifies a menu item.</li>
  <li><a href="#Check">Check</a>: Adds a visible checkmark next to a menu item.</li>
  <li><a href="#Delete">Delete</a>: Deletes a menu item or all menu items.</li>
  <li><a href="#Disable">Disable</a>: Changes a menu item to a gray color to indicate that the user cannot select it.</li>
  <li><a href="#Enable">Enable</a>: Allows the user to once again select a menu item if was previously disabled (grayed).</li>
  <li><a href="#Insert">Insert</a>: Inserts a new item before the specified item.</li>
  <li><a href="#Rename">Rename</a>: Renames a menu item.</li>
  <li><a href="#SetColor">SetColor</a>: Changes the background color of the menu.</li>
  <li><a href="#SetIcon">SetIcon</a>: Sets the icon to be displayed next to a menu item.</li>
  <li><a href="#Show">Show</a>: Displays the menu.</li>
  <li><a href="#ToggleCheck">ToggleCheck</a>: Toggles the checkmark next to a menu item.</li>
  <li><a href="#ToggleEnable">ToggleEnable</a>: Enables or disables a menu item.</li>
  <li><a href="#Uncheck">Uncheck</a>: Removes the checkmark (if there is one) from a menu item.</li>
  <li><a href="#AddStandard">AddStandard</a>: Adds the standard <a href="../Program.htm#tray-icon">tray menu items</a>.</li>
</ul>
<p><strong>General:</strong></p>
<ul>
  <li><a href="#MenuItemName">MenuItemName</a></li>
  <li><a href="#Win32_Menus">Win32 Menus</a></li>
  <li><a href="#Remarks">Remarks</a></li>
  <li><a href="#Related">Related</a></li>
  <li><a href="#Examples">Examples</a></li>
</ul>

<div class="methodShort" id="Add">
<h2>Add</h2>
<p>Adds or modifies a menu item.</p>
<pre class="Syntax">Menu.<span class="func">Add</span>(<span class="optional">MenuItemName, Callback-or-Submenu, Options</span>)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The text to display on the menu item, or the position&amp; of an existing item to modify. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
  <dt>Callback-or-Submenu</dt><dd><p>A function name or a reference to a <a href="Functor.htm">function object</a> to call as a new <a href="../misc/Threads.htm">thread</a> when the menu item is selected, or a reference to a <strong>Menu</strong> object to use as a submenu.</p>
  <p>If <em>Callback-or-Submenu</em> is omitted, <em>MenuItemName</em> will be used as both the function name and the menu item name.</p>
  <p id="CallbackParams">The function can optionally define parameters as shown below:</p>
<pre class="Syntax"><i>FunctionName</i>(ItemName, ItemPos, Menu)</pre></dd>
  <dt>Options</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
  <p>If not omitted, <em>Options</em> must be a space- or tab-delimited list of one or more of the following options:</p>
<table class="info">
  <tr><td>P<em>n</em></td>
  <td>Replace <em>n</em> with the menu item's <a href="../misc/Threads.htm">thread priority</a>, e.g. <code>P1</code>. If this option is omitted when adding a menu item, the priority will be 0, which is the standard default. If omitted when updating a menu item, the item's priority will not be changed. Use a decimal (not hexadecimal) number as the priority.</td></tr>
  <tr><td>+Radio</td>
  <td>If the item is checked, a bullet point is used instead of a check mark.</td></tr>
  <tr><td>+Right</td>
  <td>The item is right-justified within the menu bar. This only applies to <a href="../objects/Gui.htm#MenuBar">menu bars</a>, not popup menus or submenus.</td></tr>
  <tr><td>+Break</td>
  <td>The item begins a new column in a popup menu.</td></tr>
  <tr><td>+BarBreak</td>
  <td>As above, but with a dividing line between columns.</td></tr>
</table>
<p>The plus sign (+) is optional and can be replaced with minus (-) to remove the option, as in <code>-Radio</code>. Options are not case sensitive.</p>
<p>To change an existing item's options without affecting its callback or submenu, simply omit the <em>Callback-or-Submenu</em> parameter.</p>
  </dd>
</dl>
<h3>Remarks</h3>
<p>This is a multipurpose method that adds a menu item, updates one with a new submenu or callback, or converts one from a normal item into a submenu (or vice versa). If <em>MenuItemName</em> does not yet exist, it will be added to the menu. Otherwise, <em>MenuItemName</em> is updated with the newly specified <em>Callback-or-Submenu</em> and/or <em>Options</em>.</p>
<p>To add a menu separator line, omit all three parameters.</p>
<p><em>Add</em> always adds new menu items at the bottom of the menu, but <a href="#Insert">Insert</a> can be used to insert an item before an existing custom menu item.</p>
</div>

<div class="methodShort" id="Check">
<h2>Check</h2>
<p>Adds a visible checkmark in the menu next to <em>MenuItemName</em> (if there isn't one already).</p>
<pre class="Syntax">Menu.<span class="func">Check</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="ClickCount">
<h2>ClickCount</h2>
<p>Retrieves or sets the number of clicks required to activate the tray menu's default item.</p>
<pre class="Syntax">Menu.ClickCount := Count</pre>
<dl>
  <dt>Count</dt><dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>Specify 1 to allow a single-click to activate the tray menu's default menu item. Specify 2 to return to the default behavior (double-click).</p></dd>
</dl>
<p>例如: <code>A_TrayMenu.ClickCount := 1</code></p>
</div>

<div class="methodShort" id="Delete">
<h2>Delete</h2>
<p>Deletes a menu item or all custom menu items.</p>
<pre class="Syntax">Menu.<span class="func">Delete</span>(<span class="optional">MenuItemName</span>)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
<p>If <em>MenuItemName</em> is omitted, all items are deleted from the menu, leaving the menu empty. An empty menu still exists and thus any other menus that use it as a submenu will retain those submenus.</p>
<p>To delete a separator line, identify it by its position in the menu. 例如, use <code>MyMenu.Delete("3&amp;")</code> if there are two items preceding the separator.</p>
<p>If the <em>default</em> menu item is deleted, the effect will be similar to having set <code>Menu.Default := ""</code>.</p>
</div>

<div class="methodShort" id="Default">
<h2>Default</h2>
<p>Retrieves or sets the default menu item.</p>
<pre class="Syntax">CurrentDefault := Menu.Default</pre>
<dl>
  <dt>CurrentDefault</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name of the default menu item, or an empty string if there is no default.</p></dd>
</dl>
<pre class="Syntax">Menu.Default := MenuItemName</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p>
  <p>If <em>MenuItemName</em> is an empty string, there will be no default.</p></dd>
</dl>
<p>Setting the default item makes that item's font bold (setting a default item in menus other than the tray menu is currently purely cosmetic). When the user double-clicks the tray icon, its default menu item is launched (even if the item is disabled). If there is no default, double-clicking has no effect.</p>
<p>The default item for the tray menu is initially <code>&amp;Open</code>, if present. Adding <code>&amp;Open</code> to the tray menu by calling <a href="#AddStandard">AddStandard</a> or changing <a href="../Variables.htm#AllowMainWindow">A_AllowMainWindow</a> also causes it to become the default item if there wasn't one already.</p>
<p>If the default item is deleted, the menu is left without one.</p>
</div>

<div class="methodShort" id="Disable">
<h2>Disable</h2>
<p>Changes <em>MenuItemName</em> to a gray color to indicate that the user cannot select it.</p>
<pre class="Syntax">Menu.<span class="func">Disable</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="Enable">
<h2>Enable</h2>
<p>Allows the user to once again select <em>MenuItemName</em> if it was previously disabled (grayed).</p>
<pre class="Syntax">Menu.<span class="func">Enable</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="Insert">
<h2>Insert</h2>
<p>Inserts a new item before the specified item.</p>
<pre class="Syntax">Menu.<span class="func">Insert</span>(<span class="optional">ItemToInsertBefore, NewItemName, Callback-or-Submenu, Options</span>)</pre>
<dl>
  <dt>ItemToInsertBefore</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name of an existing item or a position&amp; between 1 and <a href="#Examples">the current number of custom items</a> plus 1 (following the same rules as <a href="#MenuItemName">MenuItemName</a>). Items can also be appended by omitting <em>ItemToInsertBefore</em>.</p></dd>
  <dt>NewItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The text to display on the menu item. Unlike <a href="#Add">Add</a>, this cannot be a position.</p></dd>
</dl>
<p>The remaining parameters behave as per the <a href="#Add">Add</a> method, except that <em>Insert</em> creates a new item even if <em>NewItemName</em> matches the name of an existing item.</p>
<p>As <a href="#AddStandard">Standard</a> menu items are not counted (<code>"1&amp;"</code> always refers to the first <em>custom</em> item), an item cannot be inserted immediately before the standard items.</p>
</div>

<div class="methodShort" id="Rename">
<h2>Rename</h2>
<p>Renames <em>MenuItemName</em> to <em>NewName</em>.</p>
<pre class="Syntax">Menu.<span class="func">Rename</span>(MenuItemName <span class="optional">, NewName</span>)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
  <dt>NewName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The new name. If empty or omitted, <em>MenuItemName</em> will be converted into a separator line.</p></dd>
</dl>
<p>The menu item's current callback or submenu is unchanged.</p>
<p>A separator line can be converted to a normal item by specifying the position&amp; of the separator and a non-blank <em>NewName</em>, and then using the <em>Add</em> method to give the item a callback or submenu.</p>
</div>

<div class="methodShort" id="SetColor">
<h2>SetColor</h2>
<p>Changes the background color of the menu to <em>ColorValue</em>.</p>
<pre class="Syntax">Menu.<span class="func">SetColor</span>(<span class="optional">ColorValue, Submenus := true</span>)</pre>
<dl>
  <dt>ColorValue</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>或<a href="../Concepts.htm#numbers">整数</a></p>
    <p>One of the 16 primary <a href="../misc/Colors.htm">HTML color names</a>, a hexadecimal RGB color string (the 0x prefix is optional), or a pure numeric RGB color value. Omit <em>ColorValue</em> (or specify an empty string or the word "Default") to restore the menu to its default color. Example values: <code>"Silver"</code>, <code>"FFFFAA"</code>, <code>0xFFFFAA</code>, <code>"Default"</code>.</p></dd>
  <dt>Submenus</dt><dd>
    <p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a></p>
    <p>1 (true) if the color should be applied to all of this menu's submenus, otherwise 0 (false). Defaults to 1 (true).</p></dd>
</dl>
</div>

<div class="methodShort" id="SetIcon">
<h2>SetIcon</h2>
<p>Sets the icon to be displayed next to <em>MenuItemName</em>.</p>
<pre class="Syntax">Menu.<span class="func">SetIcon</span>(MenuItemName, FileName <span class="optional">, IconNumber, IconWidth</span>)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>     <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
  <dt>FileName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The path of an icon or image file. For a list of supported formats, 请参阅 <a href="../commands/GuiControls.htm#IconSupport">the Picture control</a>.</p>
  <p>A <a href="../misc/ImageHandles.htm">bitmap or icon handle</a> can be used instead of a filename. 例如, <code>"HICON:" handle</code>.</p>
  <p>Omit <em>FileName</em> or specify an empty string or <code>"*"</code> to remove the item's current icon.</p></dd>
  <dt>IconNumber</dt><dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>To use an icon group other than the first one in the file, specify its number for <em>IconNumber</em> (if omitted, it defaults to 1). If <em>IconNumber</em> is negative, its absolute value is assumed to be the resource ID of an icon within an executable file.</p></dd>
  <dt>IconWidth</dt><dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The desired width of the icon. If the icon group indicated by <em>IconNumber</em> contains multiple icon sizes, the closest match is used and the icon is scaled to the specified size. See the Examples section for usage examples.</p></dd>
</dl>
<p>Currently it is necessary to specify "actual size" when setting the icon to preserve transparency. 例如:</p>
<pre>MyMenu.SetIcon "My menu item", "Filename.png",, 0</pre>
<p>A <a href="../misc/ImageHandles.htm">bitmap or icon handle</a> can be used instead of a filename. 例如, <code>"HBITMAP:" handle</code>.</p>
</div>

<div class="methodShort" id="Show">
<h2>Show</h2>
<p>Displays the menu, allowing the user to select an item with arrow keys, menu shortcuts (underlined letters), or the mouse.</p>
<pre class="Syntax">Menu.<span class="func">Show</span>(<span class="optional">X, Y</span>)</pre>
<dl>
  <dt>X, Y</dt><dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The coordinates at which to display the menu. If both X and Y are omitted, the menu is displayed at the current position of the mouse cursor. If only one of them is omitted, the mouse cursor's position will be used for it. X and Y are relative to the active window's client area by default. To override this default, use <code><a href="../commands/CoordMode.htm">CoordMode</a> "Menu", <i>Mode</i></code> or <code>A_CoordModeMenu := <i>Mode</i></code>.</p></dd>
</dl>
<p>Any popup menu can be shown, including submenus and the tray menu. However, an exception is thrown if <em>Menu</em> is a MenuBar object.</p>
</div>

<div class="methodShort" id="ToggleCheck">
<h2>ToggleCheck</h2>
<p>Adds a checkmark if there wasn't one; otherwise, removes it.</p>
<pre class="Syntax">Menu.<span class="func">ToggleCheck</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>     <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="ToggleEnable">
<h2>ToggleEnable</h2>
<p>Disables <em>MenuItemName</em> if it was previously enabled; otherwise, enables it.</p>
<pre class="Syntax">Menu.<span class="func">ToggleEnable</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>     <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="Uncheck">
<h2>Uncheck</h2>
<p>Removes the checkmark (if there is one) from a menu item.</p>
<pre class="Syntax">Menu.<span class="func">Uncheck</span>(MenuItemName)</pre>
<dl>
  <dt>MenuItemName</dt><dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>     <p>The name or position of a menu item. See <a href="#MenuItemName">MenuItemName</a>.</p></dd>
</dl>
</div>

<div class="methodShort" id="AddStandard">
<h2>AddStandard</h2>
<p>Adds the standard <a href="../Program.htm#tray-icon">tray menu items</a>.</p>
<pre class="Syntax">Menu.<span class="func">AddStandard</span>()</pre>
<p>This method can be used with the tray menu or any other menu.</p>
<p>The standard items are inserted after any existing items. Any standard items already in the menu are not duplicated, but any missing items are added. The table below shows the names and positions of the standard items after calling AddStandard on an empty menu:</p>
<style>
.fake-menu {
  background-color: #f2f2f2;
  border: 1px solid #cccccc;
  color: black;
  line-height: 1.4;
}
.fake-menu td {
  padding: 0 .5em;
}
.fake-menu td {
  text-align: right;
}
.fake-menu td:first-child {
  text-align: left;
}
.fake-sep {
  border: 1px solid #d7d7d7;
  border-width: 1px 0 0;
}
</style>
<table class="fake-menu">
  <tr><td>&amp;Open</td><td>1</td><td>0</td></tr>
  <tr><td>&amp;Help</td><td>2</td><td></td></tr>
  <tr><td><div class="fake-sep"></div></td><td>3</td><td></td></tr>
  <tr><td>&amp;Window Spy</td><td>4</td><td></td></tr>
  <tr><td>&amp;Reload This Script</td><td>5</td><td></td></tr>
  <tr><td>&amp;Edit This Script</td><td>6</td><td></td></tr>
  <tr><td><div class="fake-sep"></div></td><td>7</td><td></td></tr>
  <tr><td>&amp;Suspend Hotkeys</td><td>8</td><td>1</td></tr>
  <tr><td>&amp;Pause Script</td><td>9</td><td>2</td></tr>
  <tr><td>E&amp;xit</td><td>10</td><td>3</td></tr>
</table>
<p>Compiled scripts include only the last three by default. <code>&amp;Open</code> is included only if <a href="../Variables.htm#AllowMainWindow">A_AllowMainWindow</a> is 1 when AddStandard is called (in that case, add 1 to the positions shown in the third column). If the tray menu contains standard items, <code>&amp;Open</code> is inserted or removed whenever <a href="../Variables.htm#AllowMainWindow">A_AllowMainWindow</a> is changed. For other menus, <code>&amp;Open</code> has no effect if <a href="../Variables.htm#AllowMainWindow">A_AllowMainWindow</a> is 0.</p>
<p>Each standard item has an internal menu item ID corresponding to the function it performs, but can otherwise be modified or deleted like any other menu item. AddStandard detects existing items by ID, not by name. If the <a href="#Add">Add</a> method is used to change the callback function associated with a standard menu item, it is assigned a new unique ID and is no longer considered to be a standard item.</p>
<p>Adding the <code>&amp;Open</code> item to the tray menu causes it to become the default item if there wasn't one already.</p>
</div>

<div class="methodShort" id="Handle">
<h2>Handle</h2>
<p>Returns a handle to a <a href="#Win32_Menus">Win32 menu</a> (a handle of type <code>HMENU</code>), constructing it if necessary.</p>
<pre class="Syntax">Menu.Handle</pre>
<p>The returned handle is valid only until the Win32 menu is destroyed, which typically occurs when the Menu object is freed. Once the menu is destroyed, the operating system may reassign the handle value to any menus subsequently created by the script or any other program.</p>
</div>

<h2 id="MenuItemName">MenuItemName</h2>
<p>The name or position of a menu item. Some common rules apply to this parameter across all sub-commands which use it:</p>
<p>To underline one of the letters in a menu item's name, precede that letter with an ampersand (&amp;). When the menu is displayed, such an item can be selected by pressing the corresponding key on the keyboard. To display a literal ampersand, specify two consecutive ampersands as in this example: <code>"Save &amp;&amp; Exit"</code></p>
<p>When referring to an existing menu item, the name is not case sensitive but any ampersands must be included. 例如: <code>"&amp;Open"</code></p>
<p>The names of menu items can be up to 260 characters long.</p>
<p>To identify an existing item by its position in the menu, write the item's position followed by an ampersand. 例如, <code>"1&amp;"</code> indicates the first item.</p>

<h2 id="Win32_Menus">Win32 Menus</h2>
<p>Windows provides a <a href="https://msdn.microsoft.com/en-us/library/ms646977">set of functions and notifications</a> for creating, modifying and displaying menus with standard appearance and behavior. We refer to a menu created by one of these functions as a <em>Win32 menu</em>.</p>
<p>As items are added to a menu or modified, the name and other properties of each item are stored in the Menu object. A Win32 menu is constructed the first time the menu or its parent menu is attached to a GUI or shown. It is destroyed automatically when the menu object is deleted (which occurs when its reference count reaches zero).</p>
<p><a href="#Handle">Menu.Handle</a> returns a handle to a Win32 menu (a handle of type <code>HMENU</code>), constructing it if necessary.</p>
<p>Any modifications which are made to the menu directly by Win32 functions are not reflected by the script's Menu object, so may be lost if an item is modified by one of the built-in methods.</p>
<p>Each menu item is assigned an ID when it is first added to the menu. Scripts cannot rely on an item receiving a particular ID, but can retrieve the ID of an item by using GetMenuItemID as shown in <a href="#ExDllCall">示例 #5</a>. This ID cannot be used with the Menu object, but can be used with various <a href="https://msdn.microsoft.com/en-us/library/ms646977">Win32 functions</a>.</p>

<h2 id="Remarks">Remarks</h2>
<p>A menu usually looks like this:</p>
<img src="../static/ctrl_menu.png" alt="Menu" style="border: 1px solid silver;" />
<p>If a menu ever becomes completely empty -- such as by using <code>MyMenu.Delete()</code> -- it cannot be shown. If the tray menu becomes empty, right-clicking and double-clicking the tray icon will have no effect (in such cases it is usually better to use <a href="../commands/_NoTrayIcon.htm">#NoTrayIcon</a>).</p>
<p>If a menu item's callback is already running and the user selects the same menu item again, a new <a href="../misc/Threads.htm">thread</a> will be created to run that same callback, interrupting the previous thread. To instead buffer such events until later, use <a href="../commands/Critical.htm">Critical</a> as the callback's first line (however, this will also buffer/defer other threads such as the press of a hotkey).</p>
<p>Whenever a function is called via a menu item, it starts off fresh with the default values for settings such as <a href="../commands/SendMode.htm">SendMode</a>. These defaults can be changed in the <a href="../Scripts.htm#auto">auto-execute section</a>.</p>
<p>When building a menu whose contents are not always the same, one approach is to point all such menu items to the same function and have that function refer to its <a href="#CallbackParams">parameters</a> to determine what action to take. Alternatively, a <a href="Functor.htm">function object</a>, <a href="../Functions.htm#closures">closure</a> or <a href="../Variables.htm#fat-arrow">fat arrow function</a> can be used to bind one or more values or variables to the menu item's callback function.</p>

<h2 id="Related">Related</h2>
<p><a href="Gui.htm">GUI</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="../commands/Thread.htm">Thread</a>, <a href="../commands/Critical.htm">Critical</a>, <a href="../commands/_NoTrayIcon.htm">#NoTrayIcon</a>, <a href="../commands/Gosub.htm">Gosub</a>, <a href="../Functions.htm">Functions</a>, <a href="../commands/Return.htm">Return</a>, <a href="../commands/SetTimer.htm">SetTimer</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a>: This is a working script that adds a new menu item to the bottom of the tray icon menu.</p>
<pre>A_TrayMenu.Add()  <em>; Creates a separator line.</em>
A_TrayMenu.Add("Item1", "MenuHandler")  <em>; Creates a new menu item.</em>
return

MenuHandler(ItemName, ItemPos, Menu) {
    MsgBox "You selected " ItemName " (position " ItemPos ")"
}</pre>
</div>

<div class="ex" id="ExPopup">
<p><a href="#ExPopup">#2</a>: This is a working script that creates a popup menu that is displayed when the user presses the Win-Z hotkey.</p>
<pre><em>; Create the popup menu by adding some items to it.</em>
MyMenu := MenuCreate()
MyMenu.Add "Item 1", "MenuHandler"
MyMenu.Add "Item 2", "MenuHandler"
MyMenu.Add  <em>; Add a separator line.</em>

<em>; Create another menu destined to become a submenu of the above menu.</em>
Submenu1 := MenuCreate()
Submenu1.Add "Item A", "MenuHandler"
Submenu1.Add "Item B", "MenuHandler"

<em>; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is displayed.</em>
MyMenu.Add "My Submenu", Submenu1

MyMenu.Add  <em>; Add a separator line below the submenu.</em>
MyMenu.Add "Item 3", "MenuHandler"  <em>; Add another menu item beneath the submenu.</em>
return  <em>; End of script's auto-execute section.</em>

MenuHandler(Item, *) {
    MsgBox "You selected " Item
}

#z::MyMenu.Show  <em>; i.e. press the Win-Z hotkey to show the menu.</em></pre>
</div>

<div class="ex" id="ExTray">
<p><a href="#ExTray">#3</a>: This is a working script that demonstrates some of the various menu object members.</p>
<pre>#SingleInstance
global tray := A_TrayMenu <em>; For convenience.</em>
tray.delete <em>; Delete the standard items.</em>
tray.add <em>; separator</em>
tray.add "TestToggleCheck"
tray.add "TestToggleEnable"
tray.add "TestDefault"
tray.add "TestAddStandard"
tray.add "TestDelete"
tray.add "TestDeleteAll"
tray.add "TestRename"
tray.add "Test"
return

<em>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</em>

TestToggleCheck(*)
{
    tray.ToggleCheck "TestToggleCheck"
    tray.Enable "TestToggleEnable" <em>; Also enables the next test since it can't undo the disabling of itself.</em>
    tray.add "TestDelete" <em>; Similar to above.</em>
}

TestToggleEnable(*)
{
    tray.ToggleEnable "TestToggleEnable"
}

TestDefault(*)
{
    if tray.default = "TestDefault"
        tray.default := ""
    else
        tray.default := "TestDefault"
}

TestAddStandard(*)
{
    tray.addStandard
}

TestDelete(*)
{
    tray.delete "TestDelete"
}

TestDeleteAll(*)
{
    tray.delete
    MsgBox "The script may exit now, as the tray menu no longer contains custom items."
}

TestRename(*)
{
    static
    if NewName != "renamed"
    {
        OldName := "TestRename"
        NewName := "renamed"
    }
    else
    {
        OldName := "renamed"
        NewName := "TestRename"
    }
    tray.rename OldName, NewName
}

Test(Item, *)
{
    MsgBox 'You selected "' Item '"'
}</pre>
</div>

<div class="ex" id="ExIcon">
<p><a href="#ExIcon">#4</a>: This is a working script that adds icons to its menu items:</p>
<pre>FileMenu := MenuCreate()
FileMenu.Add("Script Icon", "MenuHandler")
FileMenu.Add("Suspend Icon", "MenuHandler")
FileMenu.Add("Pause Icon", "MenuHandler")
FileMenu.SetIcon("Script Icon", A_AhkPath, 2) <em>; 2nd icon group from the file</em>
FileMenu.SetIcon("Suspend Icon", A_AhkPath, -206) <em>; icon with resource ID 206</em>
FileMenu.SetIcon("Pause Icon", A_AhkPath, -207) <em>; icon with resource ID 207</em>
MyMenuBar := MenuBarCreate()
MyMenuBar.Add("&amp;File", FileMenu)
Gui := GuiCreate()
Gui.MenuBar := MyMenuBar
Gui.Add("Button",, "Exit This Example").OnEvent("Click", (*) =&gt; WinClose())
Gui.Show

MenuHandler() {
    <em>; For this example, the menu items don't do anything.</em>
}</pre>
</div>

<div class="ex" id="ExDllCall">
<p><a href="#ExDllCall">#5</a>: Retrieving item count and ID.</p>
<pre>
MyMenu := MenuCreate()
MyMenu.Add "Item 1", "no"
MyMenu.Add "Item 2", "no"
MyMenu.Add "Item B", "no"

<em>; Retrieve the number of items in a menu.</em>
item_count := DllCall("GetMenuItemCount", "ptr", MyMenu.Handle)

<em>; Retrieve the ID of the last item.</em>
last_id := DllCall("GetMenuItemID", "ptr", MyMenu.Handle, "int", item_count-1)

MsgBox "MyMenu has " item_count " items, and its last item has ID " last_id

no(*) {
    <em>; Do nothing.</em>
}
</pre>
</div>

</body>
</html>